package cn.qingyun.gis.modules.scenes.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import cn.qingyun.gis.modules.init.utils.SystemUtil;
import cn.qingyun.gis.modules.scenes.entity.PoiUserPermission;
import cn.qingyun.gis.modules.scenes.mapper.PoiUserPermissionMapper;
import cn.qingyun.gis.modules.scenes.service.PoiUserPermissionService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author LHJ
 * @description
 * @date 2024/7/1 21:32
 */
@Slf4j
@Service
@DS("scenes_offensive")
public class PoiUserPermissionServiceImpl extends ServiceImpl<PoiUserPermissionMapper, PoiUserPermission> implements PoiUserPermissionService {
    @Override
    public PoiUserPermission selectUserByTelNumber(String telNum) {
        QueryWrapper<PoiUserPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel_number",telNum);
        queryWrapper.eq("deleted",0);
        queryWrapper.last("limit 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean hasPermission() {
        log.debug("判断当前人是否有权限导入导出");
        LoginUser currentUser = SystemUtil.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new CommonException("解析用户失败");
        }
        log.debug("获取自己库里存的用户工号，{}", currentUser.getUsername());
        QueryWrapper<PoiUserPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel_number",currentUser.getUsername());
        queryWrapper.eq("deleted",0);
        queryWrapper.last("limit 1");
        PoiUserPermission poiUserPermission = this.baseMapper.selectOne(queryWrapper);
        log.debug("查出得用户数据，{}", poiUserPermission);
        if (poiUserPermission!=null){
            return true;
        }
        return false;
    }
}
